package zheezes.eisp.compute.model;

import java.math.BigDecimal;
import java.util.Calendar;

import org.apache.log4j.Logger;

import zheezes.eisp.data.orm.CalcTask;
import zheezes.eisp.data.orm.Ct;
import zheezes.eisp.data.orm.Pt;
import zheezes.eisp.data.orm.Summator;
import zheezes.eisp.data.orm.SummatorDailyFrozenData;
import zheezes.eisp.data.orm.SummatorDailyUse;
import zheezes.util.DateFormatUtil;


public class SummatorDailyUseCalcTaskHandler extends CalcTaskHandler {
	private static final Logger logger = Logger.getLogger(SummatorDailyUseCalcTaskHandler.class);
	
	public void run() {
		Summator sm = dataAccessor.getSummatorById(task.getTargetId());
		if(sm.getCp() != null && sm.getTp() != null) {
			Ct ct = dataAccessor.getCtById(sm.getCp());
			Pt pt = dataAccessor.getPtById(sm.getTp());
			if(ct != null && pt != null) {
				Calendar cal = Calendar.getInstance();
				
				cal.setTime(task.getDate());
				// next day frozen data
				SummatorDailyFrozenData cdat = dataAccessor.querySummatorDailyFrozenDataByMpIdAndDate(sm.getMpId(), cal.getTime());
				
				if(cdat != null) {
					cal.setTime(task.getDate());
					cal.add(Calendar.DATE, 1);
					SummatorDailyFrozenData ndat = dataAccessor.querySummatorDailyFrozenDataByMpIdAndDate(sm.getMpId(), cal.getTime());
					
					if(ndat != null) {
						cal.setTime(task.getDate());
						SummatorDailyUse du = dataAccessor.querySummatorDailyUseByMpIdAndDate(sm.getMpId(), cal.getTime());
						if(du == null) {
							du = new SummatorDailyUse();
						}
						du.setType(SummatorDailyUse.TYPE_SUM_METER);
						du.setMpId(sm.getMpId());
						du.setDate(cal.getTime());
						BigDecimal cval = cdat.getPp();
						BigDecimal nval = ndat.getPp();
						BigDecimal val = nval.subtract(cval);
						val = val.multiply(new BigDecimal(ct.getDeno())).divide(new BigDecimal(ct.getBase()));
						val = val.multiply(new BigDecimal(pt.getDeno())).divide(new BigDecimal(pt.getBase()));
						du.setPp(val);
						if(du.getId() == null) { /* new */
							dataAccessor.saveSummatorDailyUse(du);
						} else { /* get from db */
							dataAccessor.updateSummatorDailyUse(du);
						}
						condChecker.checkZoneLineLose(sm.getGroupId(), cal);
					} else {
						logger.info(String.format("summator[%d] has no next date[%s] frozen data", sm.getId(), DateFormatUtil.getDateFmt().format(cal.getTime())));
					}
				} else {
					logger.info(String.format("summator[%d] has no today[%s] frozen data", sm.getId(), DateFormatUtil.getDateFmt().format(cal.getTime())));
				}
			} else {
				logger.info(String.format("summator[%d] %s is not exist", sm.getId(), ct == null ? "ct" : "pt"));
			}
		} else {
			logger.info(String.format("summator[%d] %s is null", sm.getId(), sm.getCp() == null ? "ct" : "pt"));
		}
		task.setStatus(CalcTask.STATUS_FINISH);
		dataAccessor.updateCalcTask(task);
	}
	
	@Override
	public void desc() {
		logger.debug(String.format("task date = %s", DateFormatUtil.getDateFmt().format(task.getDate())));
	}
}
